import axios from "axios";

const instance = axios.create({
  // `withCredentials` Access-Control-Allow-Credentials 响应头表示是否可以将对请求的响应暴露给页面
  withCredentials: true,
  timeout: 2000,
  baseURL: "http://localhost:8888/api",
});

instance.defaults.headers.common = {};
instance.defaults.headers.post = {
  "Content-Type": "application/json",
};
instance.interceptors.request.use(
  (config) => {
    if (localStorage.getItem("token ")) {
      let token = `Bearer ${localStorage.getItem("token ")}`;
      //Authorization
      // config.headers.Authorization = token;
      config.headers!.Authorization = token;
    } else {
      // 测试写死token
      let token = 'Bearer eyJhbGciOiJIUzUxMiIsInppcCI6IkRFRiJ9.eNo8zEsOgyAURuG9_GNIykUosJsrj5QmbY1gNDHuXdtBh-cbnB289AfCjqXlGQGcXvUNgYlbWz9z-tMh8Oz1ypFMsd6TpMIsB1eSZGPvUquSVYwx-VFdg8odQVlzc05bPwjkbfoBafrCcQIAAP__.38XQQCXFkx2KB8OEMMGHz2cA4w33FEfFzFFOu5sHqEuJweec5IPFZ1bgxFsdRNIbzajSoz9F1VWnk8rM2Lkc0w';
      config.headers!.Authorization = token;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default instance;
